package com.ioidea.holland.greenHouseAct

import android.content.Context
import android.util.AttributeSet
import android.view.ViewGroup
import com.ioidea.holland.greenHouseAct.GreenHouse
import com.ioidea.holland.greenHouseAct.Point

class ScrollableGreenHouse(context: Context, attrs: AttributeSet? = null) : ViewGroup(context, attrs) {

    private val greenHouse: GreenHouse

    init {
        greenHouse = GreenHouse(context)
        addView(greenHouse)
    }

    fun setPoints(points: List<Point>) {
        greenHouse.setPoints(points)
    }

    override fun scrollTo(dx: Int, dy: Int) {
        super.scrollTo(dx, dy)
        greenHouse.scrollTo(dx, dy)
    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        greenHouse.layout(l, t, r, b)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.AT_MOST)
        val childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(heightMeasureSpec), MeasureSpec.AT_MOST)
        greenHouse.measure(childWidthMeasureSpec, childHeightMeasureSpec)
        setMeasuredDimension(greenHouse.measuredWidth, greenHouse.measuredHeight)
    }
}